<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8"/>
    <title>登录</title>
  <link rel="stylesheet" href="css/style.css">
     <!-- Vue 3 & Element Plus -->
  <script src="./public/vue.global.prod.js"></script>
  <!-- 引入 Element Plus -->
  <link rel="stylesheet" href="./public/element-plus-index.css">
  <script src="./public/index.full.js"></script>
  <link rel="stylesheet" href="./public/icon.css" />
  <!-- ECharts -->
  <script src="./public/echarts.min.js"></script>
  <script src="./public/axios.min.js"></script>
    <style>
        body {
            display: flex;
            justify-content: center;
            align-items: flex-start;
            height: 100vh;
            margin: 0;
            background-color: #fff;
        }
        #app {
            width: 350px;
            margin-top: 20vh;
        }
        .el-card {
            box-shadow: none !important;
            border: none !important;
            background: #fff !important;
        }
        .el-card__header {
            text-align: center;
            font-size: 26px;
            font-weight: bold;
            color: #222;
            padding-bottom: 10px;
        }
        .login-btn {
            width: 100%;
            background: #409eff;
            color: #fff;
            font-size: 16px;
            border-radius: 4px;
        }
        .lang-switch {
            text-align: center;
            margin-top: 16px;
            color: #409eff;
            font-size: 14px;
            cursor: pointer;
        }
        .el-input__wrapper {
            border-radius: 4px;
        }
        .el-form-item {
            margin-bottom: 18px;
        }
    </style>
</head>
<body>
<div id="app">
    <el-card>
        <template #header>
            LR云渲染管理后台
        </template>
        <el-form :model="form" :rules="rules" ref="loginForm" label-width="0">
            <el-form-item prop="username">
                <el-input v-model="form.username" placeholder="登录Id" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item prop="password">
                <el-input v-model="form.password" type="password" placeholder="密码" show-password  autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button class="login-btn" type="primary" @click="onLogin" :loading="loading">登录</el-button>
            </el-form-item>
        </el-form>
        <div class="lang-switch">
            中文 · English (US)
        </div>
    </el-card>
</div>

<script>
const { createApp, ref } = Vue;
const { ElMessage } = ElementPlus;

createApp({
    setup() {
        const form = ref({
            username: '',
            password: ''
        });
        const loading = ref(false);
        const loginForm = ref(null);

        const rules = {
            username: [
                { required: true, message: '请输入登录Id', trigger: 'blur' }
            ],
            password: [
                { required: true, message: '请输入密码', trigger: 'blur' }
            ]
        };

        function onLogin() {
            loginForm.value.validate((valid) => {
                if (!valid) return;
                loading.value = true;
                fetch('/login', {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify(form.value)
                })
                .then(res => res.json())
                .then(response => {
                    loading.value = false;
                    if (response.status === "OK") {
                        let token = response.entity.token;
                        sessionStorage.setItem('token', token);
                        sessionStorage.setItem('userName', response.entity.userData.username);
                        window.location.href = "/home.html";
                    } else {
                        ElMessage.error("登录失败: " + response.msg);
                    }
                })
                .catch(() => {
                    loading.value = false;
                    ElMessage.error("网络错误，请重试");
                });
            });
        }

        return {
            form,
            rules,
            loading,
            loginForm,
            onLogin
        };
    }
}).use(ElementPlus).mount('#app');
</script>
</body>
</html>
